<template>
    <h2>用户借款页面</h2>
    <table border="1">
        <tr>
            <td>借款款项名称</td>
            <td>{{ data.BM_Name}}</td>
        </tr>
        <tr>
            <td>最高金额</td>
            <td>{{ data.BM_Money }}</td>
        </tr>
        <tr>
            <td>利息</td>
            <td>{{ data.BMI_Interest}}</td>
        </tr>
        <tr>
            <td>借款金额</td>
            <td>
                <input type="text" @change="GetUserLorn" v-model="Jmoney">
            </td>
        </tr>
        <tr>
            <td>每月还款金额</td>
            <td>{{ Hmoney.toFixed(2) }}</td>
        </tr>
        <tr>
            <td colspan="2"><button @click="AddUserBorrow">确认借款</button></td>
        </tr>
    </table>
</template>

<script setup lang="ts">
import axios from 'axios';
import {ref,onMounted} from 'vue'
import {useRoute} from 'vue-router'
import { useUserStore } from '../../Store';
const userStore=useUserStore()
const Jmoney=ref(0)
const Hmoney=ref(0)
const route=useRoute()
const data:any=ref({})
onMounted(()=>{
    Show()
})
const Show=()=>{
    axios.get("https://localhost:44358/api/Borrow/GetUserBorrowInfo",
        {
            params:{
                Bid:route.query.BM_Id
            }
        }
    ).then(res=>{
        data.value=res.data[0]
        console.log(res.data);
        console.log(data.value);
    }).catch(err=>{
        console.log(err);
    })}
    const GetUserLorn=()=>{
        axios.get("https://localhost:44358/api/Borrow/GetUserLorn",
            {
                params:{
                    UBmoney:Jmoney.value,
                    BMI_Interest:data.value.BMI_Interest,
                    BMI_Year:data.value.BMI_Year
                }
            }
        ).then(res=>{
            Hmoney.value=res.data
            console.log(res.data);
        }).catch(err=>{
            console.log(err);
        })
    }
const AddUserBorrow=()=>{
    if(Jmoney.value>data.value.BM_Money){
        alert("金额超过最高金额")
        return
    }
    if(Jmoney.value<0){
        alert("金额不能为负数")
        return
    }
    if(Jmoney.value==0){
        alert("金额不能为0")
        return
    }
    axios.post("https://localhost:44358/api/Borrow/AddUserBorrow",
        {
            U_Id:userStore.Uid,
            BM_Id:route.query.BM_Id,
            UBM_Money:Jmoney.value
        }
    ).then(res=>{
        if(res.data>0){
            alert("借款成功")
        }else{
            alert("借款失败")
        }
    }).catch(err=>{
        console.log(err);
    })
}
</script>

